package com;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * @author Wes Billman
 * Created: Nov 28, 2006
 */
public class WowAceSettings {
    private final String DEFAULT_PROPERTIES_PATH = "defaultProperties";
    private final String USER_PROPERTIES_PATH = "userProperties";
    
    public final String WOWACE_FILES_URL = "wowAceFilesURL";
    public final String WOW_ADDONS_PATH = "wowAddonsPath";
    public final String PROXY_ENABLED = "proxyEnabled";
    public final String PROXY_HOST = "proxyHost";
    public final String PROXY_PORT = "proxyPort";
    
    Properties defaultProps;
    Properties userProps;
    
    public WowAceSettings() {
        createDefaultProps();
        defaultProps = getDefaultProps();
    }
    
    public void loadUserSettings() {
        userProps = getUserProps();
    }
    
    public void storeUserSettings() {
        setUserProps();
    }
    
    public String getSettings(String key) {
        return userProps.getProperty(key);
    }
    
    public void setSettings(String key, String value) {
        userProps.setProperty(key, value);
    }
    
    private void createDefaultProps() {
        Properties props = new Properties();
        System.out.println("Creating Default Properties...");
        
        try {
            FileOutputStream outStream = new FileOutputStream(DEFAULT_PROPERTIES_PATH);
            props.setProperty(WOWACE_FILES_URL, "http://www.wowace.com/files/");
            props.setProperty(WOW_ADDONS_PATH, "C:\\Program Files\\World of Warcraft\\Interface\\Addons");
            props.setProperty(PROXY_ENABLED, "true");
            props.setProperty(PROXY_HOST, "192.232.119.193");
            props.setProperty(PROXY_PORT, "81");
            props.store(outStream, "---Default Settings---");
            outStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        System.out.println("Complete.");
    }
    
    private Properties getDefaultProps() {
        Properties props = new Properties();
        System.out.println("Loading Default Properties...");
        
        FileInputStream inStream;
        try {
            inStream = new FileInputStream(DEFAULT_PROPERTIES_PATH);
            props.load(inStream);
            inStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        System.out.println("Complete.");
        
        return props;
    }
    
    private Properties getUserProps() {
        Properties props = new Properties();
        props = defaultProps;
        System.out.println("Loading User Properties...");
        
        FileInputStream inStream;
        try {
            inStream = new FileInputStream(USER_PROPERTIES_PATH);
            props.load(inStream);
            inStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        System.out.println("Complete.");
        
        return props;
    }
    
    private void setUserProps() {
        System.out.println("Storing User Settings");
        
        FileOutputStream outStream;
        try {
            outStream = new FileOutputStream(USER_PROPERTIES_PATH);
            userProps.store(outStream, "---User Settings---");
            outStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        System.out.println("Complete");
    }
    
}
